{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1= 1\n",
      "1*2= 2 2*2= 4\n",
      "1*3= 3 2*3= 6 3*3= 9\n",
      "1*4= 4 2*4= 8 3*4=12 4*4=16\n",
      "1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25\n",
      "1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36\n",
      "1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49\n",
      "1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64\n",
      "1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81\n"
     ]
    }
   ],
   "source": [
    "# 第一种：一行实现\n",
    "print(\n",
    "    \"\\n\".join(\n",
    "        [\n",
    "            \" \".join([\"{}*{}={:>2}\".format(x, y, x * y) for x in range(1, y + 1)])\n",
    "            for y in range(1, 10)\n",
    "        ]\n",
    "    )\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 X 1 =  1  \n",
      "2 X 1 =  2  2 X 2 =  4  \n",
      "3 X 1 =  3  3 X 2 =  6  3 X 3 =  9  \n",
      "4 X 1 =  4  4 X 2 =  8  4 X 3 = 12  4 X 4 = 16  \n",
      "5 X 1 =  5  5 X 2 = 10  5 X 3 = 15  5 X 4 = 20  5 X 5 = 25  \n",
      "6 X 1 =  6  6 X 2 = 12  6 X 3 = 18  6 X 4 = 24  6 X 5 = 30  6 X 6 = 36  \n",
      "7 X 1 =  7  7 X 2 = 14  7 X 3 = 21  7 X 4 = 28  7 X 5 = 35  7 X 6 = 42  7 X 7 = 49  \n",
      "8 X 1 =  8  8 X 2 = 16  8 X 3 = 24  8 X 4 = 32  8 X 5 = 40  8 X 6 = 48  8 X 7 = 56  8 X 8 = 64  \n",
      "9 X 1 =  9  9 X 2 = 18  9 X 3 = 27  9 X 4 = 36  9 X 5 = 45  9 X 6 = 54  9 X 7 = 63  9 X 8 = 72  9 X 9 = 81  \n"
     ]
    }
   ],
   "source": [
    "# 第二种\n",
    "for i in list(range(1, 10)):\n",
    "    for j in list(range(1, i + 1)):\n",
    "        print(\"{} X {} = {:>2}  \".format(i, j, i * j), end=\"\")  #:>2，format功能，指右对齐两格\n",
    "    print(\"\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 X 1 = 1  \n",
      "1 X 2 = 2  2 X 2 = 4  \n",
      "1 X 3 = 3  2 X 3 = 6  3 X 3 = 9  \n",
      "1 X 4 = 4  2 X 4 = 8  3 X 4 = 12  4 X 4 = 16  "
     ]
    }
   ],
   "source": [
    "# 以下是列举思路，列举后提取总结出上述代码\n",
    "for i in list(range(1, 2)):\n",
    "    print(\"{} X {} = {}  \".format(i, 1, i * 1), end=\"\")\n",
    "\n",
    "print(\"\")\n",
    "\n",
    "for i in list(range(1, 3)):\n",
    "    print(\"{} X {} = {}  \".format(i, 2, i * 2), end=\"\")\n",
    "\n",
    "print(\"\")\n",
    "\n",
    "for i in list(range(1, 4)):\n",
    "    print(\"{} X {} = {}  \".format(i, 3, i * 3), end=\"\")\n",
    "\n",
    "print(\"\")\n",
    "\n",
    "for i in list(range(1, 5)):\n",
    "    print(\"{} X {} = {}  \".format(i, 4, i * 4), end=\"\")"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "360b5204b76eb2056cbc13af1e3693270cd50165c0a9f480bb76334f67c80658"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
